package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.campus.app.mapper.ClassRoomMapper;
import com.campus.app.service.ClassRoomsService;
import com.campus.commons.entity.ClassRoom;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import com.campus.config.lifecycle.CustomInit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 教室表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-28
 */
@Service
public class ClassRoomServiceImpl extends ServiceImpl<ClassRoomMapper, ClassRoom> implements ClassRoomsService {
    @Autowired
    private CourseServiceImpl courseService;
    @Autowired
    private ClassRoomMapper classRoomMapper;
    @Autowired
    private CustomInit customInit;

    @Override
    public Result<List<ClassRoom>> getAll(int size, int limit) {

        return Result.result(classRoomMapper.selectPage(new Page<ClassRoom>(size, limit), null));
    }

    @Override
    public Result<List<ClassRoom>> getAllByLike(int size, int limit, String classRoomName) {
        QueryWrapper<ClassRoom> wrapper = new QueryWrapper<>();
        wrapper.like("roomName", classRoomName);
        Result success = Result.result(classRoomMapper.selectPage(new Page<ClassRoom>(size, limit), wrapper));
        return success;
    }

    @Override
    public Result add(@NotNull ClassRoom classRoom) {
        int insert = classRoomMapper.insert(classRoom);
        if (insert == 1) {
            customInit.setClassRoom();
        return Result.success(insert);

        }
        return Result.error(Error.System, insert);
    }

    @Override
    public Result del(@Min(1) Integer id) {
        int i = classRoomMapper.deleteById(id);
        if (i == 1) {
            customInit.setClassRoom();
            return Result.success(i);
        }

        return Result.error(Error.System, i);
    }

    @Override
    public Result upDate(@NotNull ClassRoom classRoom) {

        int update = classRoomMapper.updateById(classRoom);
        if (update == 1) {
            customInit.setClassRoom();
            return Result.success(update);
        }
        return Result.error(Error.System, update);
    }

    @Override
    public Result<List<ClassRoom>> getClassRoomIsNullByDate(MyPage myPage, @NotNull String date) {
        QueryWrapper<ClassRoom> wrapper = new QueryWrapper<>();
        wrapper.notInSql("id", "select classroomId from course where data='" + date + "'");
        wrapper.eq("date", date);
        return Result.success(classRoomMapper.selectPage(new Page<ClassRoom>(myPage.getLimit(), myPage.getPage()), wrapper));
    }

    @Override
    public Boolean exitClassRoomByName(String classRoomName) {
        QueryWrapper<ClassRoom> wrapper = new QueryWrapper<>();
        wrapper.eq("roomName", classRoomName);
        Integer count = classRoomMapper.selectCount(wrapper);
        return count == 1;
    }
}
